#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'hhuua'

import aiomysql
import logging
from config import config
logging.basicConfig(level=logging.ERROR)

mysql_pool = None


async def create_db_pool(loop):
    global mysql_pool
    mysql_pool = await create_pool(loop, **config['db'])


async def create_pool(loop, **kw):
    pool = await aiomysql.create_pool(
            host=kw.get('host', 'localhost'),
            port=kw.get('port', 3306),
            user=kw['user'],
            password=kw['password'],
            db=kw['db'],
            charset=kw.get('charset', 'utf8'),
            autocommit=kw.get('autocommit', True),
            maxsize=kw.get('maxsize', 10),
            minsize=kw.get('minsize', 1),
            loop=loop,
        )
    return pool


def get_mysql_pool():
    global mysql_pool
    return mysql_pool

